package org.iabako.server.tools;

import org.iabako.shared.entity.enumeration.Language;
import org.iabako.shared.tools.DateTools;
import org.iabako.shared.tools.GenericTools;

import java.text.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by jose on 07/11/14.
 */
public class ServerTools implements DateTools {

    public Date addDaysToDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    public Date addMonthsToDate(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

    public Date addYearsToDate(Date date, int years) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);
        return calendar.getTime();
    }

    public Date resetTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static String formatAmount(Language language, Double value) {
        if (value == null) {
            return null;
        }
        DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(new Locale(language.getLangString()));

        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setGroupingSeparator(' ');
        symbols.setDecimalSeparator(formatter.getDecimalFormatSymbols().getDecimalSeparator());

        formatter.setDecimalFormatSymbols(symbols);
        formatter.setMaximumFractionDigits(2);
        formatter.setMinimumFractionDigits(2);

        return formatter.format(value);
    }

    public static Date getDateFromApacheLogs(String dateFromApache) {
        //ex. 11/May/2015:05:26:00
        DateFormat format = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);
        try {
            return format.parse(dateFromApache);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getDateFromPostFixLogs(String dateFromPostFix) {
        //ex. May 18 00:43:57
        DateFormat format = new SimpleDateFormat("MMM dd HH:mm:ss", Locale.ENGLISH);
        try {
            //In postfix log file, year is not present
            Date date = format.parse(dateFromPostFix);
            date.setYear(new Date().getYear());
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    public static Double round(Double value) {
        if (GenericTools.isEmpty(value)) {
            return null;
        }
        //Language is not important here (use EN by default)
        return GenericTools.parseAmount(formatAmount(Language.EN, value));
    }
}
